﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Providers.Entities;
using System.Web.Routing;
using CNav.Model;

namespace CNav.Support
{
    public class AdminAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var sesssionUser = httpContext.Session[Constant.SessionUser];
            if (sesssionUser != null)
            {
               return ((SessionUser)sesssionUser).IsBusinessAdmin;
            }
            return false;
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAuthenticated)
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
            else
                base.HandleUnauthorizedRequest(filterContext);
        }
    }
}